package com.mit.service.purchase.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.purchase.dto.ColorStyleDTO;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.purchase.dao.IColorStyleDAO;

public class ColorStyleDAOImpl extends AbstractDAO implements IColorStyleDAO {

	@Override
	public List<ColorStyleDTO> retrieveColorStyles(Integer material,String activeIndicator) {
		HashMap param = new HashMap();
		param.put("materialId", material);
		param.put("actInd", activeIndicator);
		return (List<ColorStyleDTO>) getSqlMapClientTemplate().queryForList("selectColorStylesByMaterial", param);
	}

	@Override
	public ColorStyleDTO createColorStyle(ColorStyleDTO to) {
		Integer id = (Integer) getSqlMapClientTemplate().insert("insertColorStyle", to);
		to.setId( id);
		return to;
	}

	@Override
	public ColorStyleDTO updateColorStyle(ColorStyleDTO to)
			throws NoRecordUpdateException {
		int rowCount = getSqlMapClientTemplate().update("updateColorStyle", to);
		if(rowCount < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public boolean hasColorStyle(Integer material, String code, String name,
			Integer excludedId) {
		HashMap param = new HashMap();
		param.put("materialId", material);
		param.put("name", name);
		param.put("code",code);
		param.put("id", excludedId);
		int rowCount = (Integer) getSqlMapClientTemplate().queryForObject("selectColorStyleCountByCodeOrName", param);
		return rowCount > 0;
	}

	@Override
	public ColorStyleDTO retrieveColorStyle(Integer id) {
		
		return (ColorStyleDTO) getSqlMapClientTemplate().queryForObject("selectColorStyleById", id);
	}

}
